package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.SystemConfigMapper;
import org.linlinjava.litemall.db.models.SystemConfig;
import org.linlinjava.litemall.db.service.SystemConfigService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig>
        implements SystemConfigService {

    private Map<String, String> transform(List<SystemConfig> systemList){
        Map<String, String> systemConfigs = new HashMap<>();
        for (SystemConfig item : systemList) {
            systemConfigs.put(item.getKeyName(), item.getKeyValue());
        }
        return systemConfigs;
    }

    @Override
    public Map<String, String> queryAll() {
        QueryWrapper<SystemConfig> query = new QueryWrapper<>();
        List<SystemConfig> systemList = baseMapper.selectList(query);
        return transform(systemList);
    }

    @Override
    public Map<String, String> listMail() {
        QueryWrapper<SystemConfig> query = new QueryWrapper<>();
        query.like("key_name", "litemall_mall_");
        List<SystemConfig> systemList = baseMapper.selectList(query);
        return transform(systemList);
    }

    @Override
    public Map<String, String> listWx() {
        QueryWrapper<SystemConfig> query = new QueryWrapper<>();
        query.like("key_name", "litemall_wx_");
        List<SystemConfig> systemList = baseMapper.selectList(query);
        return transform(systemList);
    }

    @Override
    public Map<String, String> listOrder() {
        QueryWrapper<SystemConfig> query = new QueryWrapper<>();
        query.like("key_name", "litemall_order_");
        List<SystemConfig> systemList = baseMapper.selectList(query);
        return transform(systemList);
    }

    @Override
    public Map<String, String> listExpress() {
        QueryWrapper<SystemConfig> query = new QueryWrapper<>();
        query.like("key_name", "litemall_express_");
        List<SystemConfig> systemList = baseMapper.selectList(query);
        return transform(systemList);
    }

    @Override
    public void updateConfig(Map<String, String> data) {
        for (Map.Entry<String, String> entry : data.entrySet()) {
            QueryWrapper<SystemConfig> query = new QueryWrapper<>();
            query.eq("key_name", entry.getKey());

            SystemConfig system = baseMapper.selectOne(query);
            if(null != system) {
                system.setKeyName(entry.getKey());
                system.setKeyValue(entry.getValue());
                baseMapper.updateById(system);
            }
        }

    }

    @Override
    public void addConfig(String key, String value) {
        SystemConfig system = new SystemConfig();
        system.setKeyName(key);
        system.setKeyValue(value);
        baseMapper.insert(system);
    }
}
